<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class message_db extends Model{
	public $pk = 'message_id';
	public $table = 'ptx_message';
	var $linker = array(
		'fromuser'=>array(
			'type' => 'hasone',  
			'map' => 'fromuser',
			'mapkey' => 'from_user_id', 
			'ftable' => 'ptx_user',
			'fkey' => 'user_id',
			'enabled' => true
		),
		'touser'=>array(
			'type' => 'hasone',  
			'map' => 'touser',
			'mapkey' => 'to_user_id', 
			'ftable' => 'ptx_user',
			'fkey' => 'user_id',
			'enabled' => true
		)
	);

	var $select_fields = " ptx_message.*,fromuser.user_id as fuser_id,fromuser.nickname as fnickname,touser.user_id as tuser_id,touser.nickname as tnickname ";

	private function init_conditions($conditions){
		$conditions_message = NULL;
		if(isset($conditions['from_user_id'])){
			$user_id = $this->escape($conditions['from_user_id']);
			$conditions_message .= "AND ptx_message.from_user_id={$user_id} ";
		}
		if(isset($conditions['to_user_id'])){
			$user_id = $this->escape($conditions['to_user_id']);
			$conditions_message .= "AND ptx_message.to_user_id={$user_id} ";
		}
		if(strpos($conditions_message, 'AND') === 0){
			$conditions_message = substr($conditions_message, 3);
		}
		return $conditions_message;
	}
	
	public function search($conditions=NULL,$page,$pagesize,$fields = null,$sort=null){
		if(is_array($conditions)) $conditions = $this->init_conditions($conditions);
		$sort = isset($sort)?$sort:" ptx_message.message_id DESC ";
		$fields = isset($fields)?$fields:$this->select_fields;
		return $this->pager($page, $pagesize)->findAllJoin($conditions,$sort,$fields);
	}
	
	public function search_no_page($conditions=NULL,$fields = null,$sort=null,$limit){
		if(is_array($conditions)) $conditions = $this->init_conditions($conditions);
		$sort = isset($sort)?$sort:" ptx_message.message_id DESC ";
		$fields = isset($fields)?$fields:$this->select_fields;
		return $this->findAllJoin($conditions,$sort,$fields,$limit);
	}
	
	public function find_one($conditions=NULL){
		return $this->findJoin($conditions);
	}

	public function clean_message($user_id,$start_id){
		$user_id = $this->escape($user_id);
		$start_id = ($start_id)?$this->escape($start_id):$this->escape(0);
		$this->delete(" to_user_id={$user_id} AND message_id<{$start_id} ");
	}
	
	public function clean_message_num($user_id,$num){
		$message = $this->findSql("SELECT message_id from {$this->tbl_name} WHERE to_user_id='{$user_id}' ORDER BY message_id DESC LIMIT {$num},1");
		if($message&&$message['message_id']){
			$this->delete(" to_user_id={$user_id} AND message_id<{$message['message_id']} ");
		}
	}

	public function add_one($data){
		if($this->check_value($data)){
			$data['create_time'] = TIMESTAMP;
			$id = $this->create($data);
			return $id;
		}
		return false;
	}


	private function check_value($data){
		if(!is_numeric($data['from_user_id'])||!is_numeric($data['to_user_id'])){
			return false;
		}
		return true;
	}

}
